package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressMapper;
import com.sky.result.Result;
import com.sky.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper addressMapper;

    @Override
    public void save(AddressBook addressBook) {
        AddressBook addressBook1 = AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .isDefault(1)
                .build();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        List<AddressBook> list = addressMapper.select(addressBook1);
        if (!list.isEmpty()) {
            addressBook.setIsDefault(0);
        }
        addressMapper.save(addressBook);
    }

    @Override
    public List<AddressBook> requestAddress(int i) {
        //查询所有信息
        if (i == 0) {
            AddressBook addressBook = AddressBook.builder()
                    .userId(BaseContext.getCurrentId())
                    .build();
            return addressMapper.select(addressBook);
        }

        //查询默认信息
        else {
            AddressBook addressBook = AddressBook.builder()
                    .userId(BaseContext.getCurrentId())
                    .isDefault(1)
                    .build();
            return addressMapper.select(addressBook);
        }

    }

    @Override
    public void updateAddress(AddressBook addressBook) {
        addressMapper.update(addressBook);
    }

    @Override
    public AddressBook getAddress(Long id) {
        AddressBook addressBook = AddressBook.builder()
                .id(id)
                .build();
        return addressMapper.select(addressBook).get(0);
    }

    @Override
    public void deleteAddress(Long id) {
        AddressBook temp = new AddressBook();
        List<AddressBook> list = addressMapper.select(temp);
        //判断是否有多个地址
        if(list.size() > 1){
            temp.setIsDefault(1);
            AddressBook addressBook = addressMapper.select(temp).get(0);
            addressMapper.deleteById(id);
            //如果当前删掉的地址为默认地址，则顺延默认地址
            if(Objects.equals(addressBook.getId(), id)){
                for (AddressBook book : list) {
                    if (!Objects.equals(book.getId(), id)) {
                        temp.setId(book.getId());
                        addressMapper.update(temp);
                        break;
                    }
                }
            }
        }
        else {
            addressMapper.deleteById(id);
        }

    }

    @Override
    public void setDefalutAddress(Long id) {
        AddressBook addressBook = AddressBook.builder()
                .isDefault(1)
                .build();
        Long PostAddressId = addressMapper.select(addressBook).get(0).getId();
        //将新的地址设置为默认地址
        addressBook.setId(id);
        addressMapper.update(addressBook);
        //将旧的地址取消默认地址
        addressBook.setId(PostAddressId);
        addressBook.setIsDefault(0);
        addressMapper.update(addressBook);
    }
}
